package com.basin.micro.admin.module.department.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.basin.micro.admin.module.department.pojo.entity.DepartmentEntity;
import com.basin.micro.admin.module.department.pojo.enums.DeptLevelEnum;
import com.basin.micro.admin.module.department.pojo.vo.DepartLevelInfoVO;
import com.basin.micro.admin.module.department.pojo.vo.DepartTreeVO;
import com.basin.micro.admin.module.department.repository.DepartmentRepository;
import com.basin.micro.admin.module.department.service.DepartmentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: Sam ZHONG | sammy1997@aliyun.com
 */
@Service
@Slf4j(topic = "BASIN:ADMIN:DEPARTMENT_SERVICE")
@RequiredArgsConstructor
public class DepartmentServiceImpl implements DepartmentService {
    private final DepartmentRepository departmentRepository;

    @Override
    public DepartmentEntity getByCode(String code) {
        return departmentRepository.getBaseMapper().selectOne(Wrappers.<DepartmentEntity>lambdaQuery()
                .eq(DepartmentEntity::getDeptCode, code));
    }

    @Override
    public List<DepartTreeVO> getDepartmentTree() {
        List<DepartmentEntity> entityList = this.departmentRepository.list();
        return DepartTreeVO.build(entityList);
    }

    @Override
    public List<DepartLevelInfoVO> getDepartmentLevelInfo() {
        return Arrays.stream(DeptLevelEnum.values())
                .map(enumItem -> DepartLevelInfoVO.builder()
                        .levelValue(enumItem.getLevel())
                        .levelLabel(enumItem.getLabel())
                        .build()).collect(Collectors.toList());
    }
}
